package com.summer.learn.handlers;

import com.summer.learn.dao.DepartmentDao;
import com.summer.learn.dao.EmployeeDao;
import com.summer.learn.modle.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

@Controller
public class EmployeeHandler {

    @Autowired
    private EmployeeDao employeeDao;

    @Autowired
    private DepartmentDao departmentDao;

    @RequestMapping("employees")
    public String list(Map<String, Object> map) {
        map.put("employees", employeeDao.getAll());
        return "list";
    }

    @RequestMapping(value = "employee", method = RequestMethod.GET)
    public String input(Map<String, Object> map) {
        map.put("departments", departmentDao.getDepartments());
        map.put("employee", new Employee());
        return "input";
    }

    @RequestMapping(value = "employee/{id}", method = RequestMethod.GET)
    public String input(@PathVariable("id") int id, Map<String, Object> map) {
        map.put("departments", departmentDao.getDepartments());
        map.put("employee", employeeDao.get(id));
        return "input";
    }

    /**
     * 在数据从表单转换到 pojo对象时，对数据格式化过程出现错误异常后会被保存到 BindingResult 中
     * 所以需要处理错误异常可以在目标方法的参数列表中加入 BindingResult 参数来传递消息处理，
     */
    @RequestMapping(value = "employee", method = RequestMethod.POST)
    public String save(@Valid Employee employee, BindingResult result){
        System.out.println("save： " + employee);

        if(result.getFieldErrorCount() > 0) {
            System.out.println("save： 出错了");
            for(FieldError error : result.getFieldErrors()) {
                System.out.println(error.getField() + " => " + error.getDefaultMessage());
            }
        }

        employeeDao.save(employee);
        return "redirect:/employees";
    }

    @RequestMapping(value = "employee/{id}", method = RequestMethod.DELETE)
    public String delete(@PathVariable("id") int id){
        employeeDao.delete(id);
        return "redirect:/employees";
    }

    @ModelAttribute
    public void getEmployee(@RequestParam(value = "id", required = false) Integer id, Map<String, Object> map) {
        System.out.println("ModelAttribute===========> getEmployee :" + id);
        if(id != null) {
            map.put("employee", employeeDao.get(id));
        }
    }

    @RequestMapping(value = "employee", method = RequestMethod.PUT)
    public String update(Employee employee) {
        employeeDao.save(employee);
        return "redirect:employees";
    }

    /**
     * 由 @InitBinder 标识的方法，可以对 WebDataBinder 对象进行初始化。
     * WebDataBinder 是 DataBinder 的子类，用于完成由表单字段到 JavaBean 属性的绑定
     * @InitBinder 方法不能有返回值，它必须声明为 void。
     * @InitBinder 方法的参数通常是 WebDataBinder
     */
//    @InitBinder
//    public void initBinder(WebDataBinder binder) {
//        binder.setDisallowedFields("lastName");
//    }
}
